//
//  UIViewController+Category.swift
//  SimpleTZ
//
//  Created by wangfeng on 2020/7/13.
//  Copyright © 2020 davis. All rights reserved.
//

import UIKit

extension UIViewController {
    // MARK: - 查找顶层控制器
    /// 获取顶层控制器 根据window
    
    static func appearController() -> (UIViewController?) {
        var window = UIApplication.shared.keyWindow
               //是否为当前显示的window
               if window?.windowLevel != UIWindow.Level.normal {
                   let windows = UIApplication.shared.windows
                   for  windowTemp in windows{
                       if windowTemp.windowLevel == UIWindow.Level.normal{
                           window = windowTemp
                           break
                       }
                   }
               }
               let vc = window?.rootViewController
               return appearController(currentVC: vc)
    }
    
    /// 根据控制器获取 顶层控制器
    static func appearController(currentVC VC: UIViewController?) -> UIViewController? {
        
        if VC == nil {
            return nil
        }
        
        if let presentVC = VC?.presentedViewController {
            //modal出来的 控制器
            return appearController(currentVC: presentVC)
        }else if let tabVC = VC as? UITabBarController {
            if let selectVC = tabVC.selectedViewController {
                return appearController(currentVC: selectVC)
            }
            return nil
        } else if let naiVC = VC as? UINavigationController {
            // 控制器是 nav
            return appearController(currentVC: naiVC.visibleViewController)
        } else {
            // 返回顶控制器
            return VC
        }
    }
    
}
